//
//  YCCameraBottomView.swift
//  YCCamera
//
//  Created by Billy on 6/1/25.
//

import UIKit
import AVFoundation

protocol YCCameraBottomViewDelegate: AnyObject {
    
    /// 拍照
    func cameraDidClick(_bottomView: YCCameraBottomView)
    /// 闪光灯
    func flashDidClick(_bottomView: YCCameraBottomView, isOn: Bool)
    /// 定时
    func timeSelectDidClick(_bottomView: YCCameraBottomView)
    /// 画面比例选择
    func aspectSelectDidClick(_bottomView: YCCameraBottomView)
    /// 切换摄像头
    func switchCameraDidClick(_bottomView: YCCameraBottomView)
    /// 滤镜选择
    func filterDidClick(_bottomView: YCCameraBottomView)
    /// 最新照片
    func latestPhotoTapped(_bottomView: YCCameraBottomView)
}

class YCCameraBottomView: UIView {
    
    weak var delegate: YCCameraBottomViewDelegate?
    
    /// 拍照按钮
    private lazy var cameraBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.layer.masksToBounds = true
        btn.setImage(UIImage(named: "yc_takePhoto_unsel"), for: .normal)
        btn.setImage(UIImage(named: "yc_takePhoto_sel"), for: .highlighted)
        btn.addTarget(self, action: #selector(cameraBtnClick), for: .touchUpInside)
        
        return btn
    }()
    
    /// 闪光灯
    public lazy var flashBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "camera_flash_off"), for: .normal)
        btn.setImage(UIImage(named: "camera_flash_on"), for: .selected)
        btn.addTarget(self, action: #selector(flashBtnClick(button:)), for: .touchUpInside)

        return btn
    }()
    
    /// 定时按钮
    private lazy var timerSelectBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "camera_time"), for: .normal)
        button.addTarget(self, action: #selector(timeSelectAction), for: .touchUpInside)
        
        return button
    }()
    
    /// 画面比例选择按钮
    public lazy var aspectSelectBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitleColor(.white, for: .normal)
        button.setTitle("4:3", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        button.addTarget(self, action: #selector(aspectSelectAction), for: .touchUpInside)
        
        return button
    }()
    
    /// 切换
    public lazy var switchCameraBtn: UIButton = {
        let cameraCount = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .unspecified).devices.count
        
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "camera_switch"), for: .normal)
        btn.addTarget(self, action: #selector(switchCameraBtnClick), for: .touchUpInside)
        btn.isHidden = cameraCount <= 1
        
        return btn
    }()
    
    /// 滤镜选择按钮
    private lazy var filterSelectBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "camera_filter"), for: .normal)
        button.addTarget(self, action: #selector(filterAction), for: .touchUpInside)
        
        return button
    }()
    
    // 最新照片
    public lazy var latestPhoto: UIImageView = {
        let view = UIImageView()
        view.contentMode = .scaleAspectFill
        view.layer.cornerRadius = 8
        view.layer.masksToBounds = true
        view.isUserInteractionEnabled = true
        view.backgroundColor = UIColor(white: 0.1, alpha: 1.0)
        view.layer.borderWidth = 1.5
        view.layer.borderColor = UIColor.white.cgColor
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(photoTapped))
        view.addGestureRecognizer(tapGesture)
        
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.yc.rgba(27, 27, 27)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(cameraBtn)
        addSubview(latestPhoto)
        addSubview(filterSelectBtn)
        addSubview(flashBtn)
        addSubview(aspectSelectBtn)
        addSubview(timerSelectBtn)
        addSubview(switchCameraBtn)
        
        let buttonWidth: CGFloat = 40
        let buttonMargin: CGFloat = (ScreenWidth - buttonWidth * 4) / 5
        
        cameraBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-BottomSafeHeight - 30)
            make.width.height.equalTo(64)
        }
        latestPhoto.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.centerY.equalTo(cameraBtn)
            make.width.height.equalTo(60)
        }
        filterSelectBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.centerY.equalTo(cameraBtn)
            make.width.height.equalTo(60)
        }
        timerSelectBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(buttonMargin)
            make.bottom.equalTo(cameraBtn.snp.top).offset(-20)
            make.width.height.equalTo(buttonWidth)
        }
        aspectSelectBtn.snp.makeConstraints { make in
            make.left.equalTo(timerSelectBtn.snp.right).offset(buttonMargin)
            make.bottom.equalTo(timerSelectBtn)
            make.width.height.equalTo(buttonWidth)
        }
        flashBtn.snp.makeConstraints { make in
            make.left.equalTo(aspectSelectBtn.snp.right).offset(buttonMargin)
            make.bottom.equalTo(timerSelectBtn)
            make.width.height.equalTo(buttonWidth)
        }
        switchCameraBtn.snp.makeConstraints { make in
            make.left.equalTo(flashBtn.snp.right).offset(buttonMargin)
            make.bottom.equalTo(timerSelectBtn)
            make.width.height.equalTo(buttonWidth)
        }
    }
    
    @objc private func cameraBtnClick() {
        delegate?.cameraDidClick(_bottomView: self)
    }
    
    @objc private func flashBtnClick(button: UIButton) {
        button.isSelected.toggle()
        delegate?.flashDidClick(_bottomView: self, isOn: button.isSelected)
    }
    
    @objc private func timeSelectAction() {
        delegate?.timeSelectDidClick(_bottomView: self)
    }
    
    @objc private func aspectSelectAction() {
        delegate?.aspectSelectDidClick(_bottomView: self)
    }
    
    @objc private func switchCameraBtnClick() {
        delegate?.switchCameraDidClick(_bottomView: self)
    }
    
    @objc private func filterAction() {
        delegate?.filterDidClick(_bottomView: self)
    }
    
    @objc private func photoTapped() {
        delegate?.latestPhotoTapped(_bottomView: self)
    }


}

